package com.example.taobaounion.view.activity.impl;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseActivity;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.view.activity.IMainActivity;
import com.example.taobaounion.view.fragment.HomeFragment;
import com.example.taobaounion.view.fragment.OnSellFragment;
import com.example.taobaounion.view.fragment.SearchFragment;
import com.example.taobaounion.view.fragment.SelectedFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements IMainActivity {


    public static final String TAG = "MainActivity";

    // View
    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView mNavigationView;

    // Fragment
    private SearchFragment mSearchFragment;
    private OnSellFragment mOnSellFragment;
    private SelectedFragment mSelectedFragment;
    private HomeFragment mHomeFragment;
    private FragmentManager mFragmentManager;


    private BaseFragment lastOneFragment = null;


    @Override
    protected void initContentView() {
        setContentView(R.layout.activity_main);
    }

    protected void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(item -> {

            LogUtils.d(TAG, "title --->" + item.getTitle());

            switch (item.getItemId()) {
                case R.id.home:
//                        LogUtils.d(this, "Home");
                    switchFragment(mHomeFragment);
                    break;
                case R.id.search:
//                        LogUtils.d(this, "Search");
                    switchFragment(mSearchFragment);
                    break;
                case R.id.red_packet:
//                        LogUtils.d(this, "red");
                    switchFragment(mOnSellFragment);
                    break;
                case R.id.selected:
//                        LogUtils.d(this, "selected");
                    switchFragment(mSelectedFragment);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + item.getItemId());
            }
            return true;
        });
    }


    private void switchFragment(BaseFragment fragment) {

        // 如果上一个fragment 和需要切换的fragment是同一个。这不需要操作
        if (lastOneFragment == fragment) {
            return;
        }

        // 修改成add和Hide方式来控制Fragment
        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        if (!fragment.isAdded()) {
            transaction.add(R.id.main_page_container, fragment);
        }


        transaction.show(fragment);

        if (lastOneFragment != null) {
            transaction.hide(lastOneFragment);
        }

        lastOneFragment = fragment;


        // 如果能缓存肯定比临时替换快
        // if have big cache must better the no have
//        transaction.replace(R.id.main_page_container, fragment);

        transaction.commit();
    }

    protected void initVariable() {
        mFragmentManager = getSupportFragmentManager();
    }

    protected void initFragment() {
        mHomeFragment = new HomeFragment();
        mSearchFragment = new SearchFragment();
        mOnSellFragment = new OnSellFragment();
        mSelectedFragment = new SelectedFragment();

        lastOneFragment = mHomeFragment;
    }

    protected void initView() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();

        transaction.add(R.id.main_page_container, mHomeFragment);

        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }


    /**
     * 切换到搜索界面的方法
     */
    public void switch2SearchFragment() {
        mNavigationView.setSelectedItemId(R.id.search);
    }
}